//
//  UserAvatar.m
//  GOSProject
//
//  Created by yeyifeng on 16/12/27.
//  Copyright © 2016年 yeyifeng. All rights reserved.
//

#import "UserAvatar.h"

@implementation UserAvatar


-(instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        self.font = [UIFont systemFontOfSize:15];
        self.backgroundColor = [UIColor colorWithHexString:@"4EDBCE"];
        self.textColor = [UIColor whiteColor];
        self.avtarImage = [[UIImageView alloc]initWithFrame:self.bounds];
        self.avtarImage.contentMode = UIViewContentModeScaleAspectFill;
        self.layer.masksToBounds = YES;
        self.textAlignment = 1;
        self.layer.cornerRadius = frame.size.width/2;
        [self addSubview:self.avtarImage];
        
        self.userInteractionEnabled = YES;
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
        [self addGestureRecognizer:tap];
    }
    return self;
}

- (void)tap {
    if (self.avtarTap) {
        self.avtarTap();
    }
}

-(void)setFrame:(CGRect)frame{

    [super setFrame:frame];

}

-(void)setName:(NSString *)name andImageUrl:(NSString *)url{
    
    if ([[BSTool sharedBSTool] isChineseWithStr:name]||[[BSTool sharedBSTool] includeChineseWithStr:name]) {
        if (name.length>2) {
            self.text = [name substringFromIndex:name.length - 2];
        }else{
            self.text = name;
        }
    }else{
        
        if (name.length>2) {
            self.text = [name substringToIndex:2];
        }else{
            self.text = name;
        }
    }

    if (![[url lastPathComponent]isEqualToString:@"avatar1.png"]&&![[url lastPathComponent]isEqualToString:@"avatar0.png"]) {
        
        self.avtarImage.hidden = NO;
        [self.avtarImage sd_setImageWithURL:[NSURL URLWithString:url]];
        
    }else{
        self.avtarImage.hidden = YES;
    }
}

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
